package org.example.spring;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

import java.text.SimpleDateFormat;

/**
 * Spring的配置类
 *
 * @author bobo
 * @since 2024/4/26
 */
@Configuration
@ComponentScan(basePackages = "org.example.*")
public class SpringConfig {

	@Bean
	public ObjectMapper objectMapper(){
		ObjectMapper mapper = new ObjectMapper();
		//在反序列化时忽略在 json 中存在但 Java 对象不存在的属性
		mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
		//在序列化时日期格式默认为 yyyy-MM-dd'T'HH:mm:ss.SSSZ
		mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
		//在序列化时自定义时间日期格式
		mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
		//在序列化时忽略值为 null 的属性
		mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
		//在序列化时忽略值为默认值的属性
		mapper.setDefaultPropertyInclusion(JsonInclude.Include.NON_DEFAULT);
		return mapper;
	}

}
